package com.itbour.web.action.yjx.tmpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.util.StringUtils;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.yjx.tmpl.YjxShowType;
import com.itbour.service.yjx.tmpl.YjxTmplTypeService;
import com.opensymphony.xwork2.ActionContext;

/**		@author 李琦
 * 		一键秀 分类管理
 */
public class YjxTmplTypeAction extends BaseAction{

	/**
	 */
	private static final long serialVersionUID = 1L;
	private YjxTmplTypeService yjxTmplTypeService;// 分类管理
	private String findPubFlg;// 分类发布状态
	private String findTypeTitle;// 分类标题
	private String findTypeStId;//模板组ID
	
	
	
	// 分页获取分类信息
	private Map<String, Object> tmplTypeMap =new HashMap<String, Object>();
	public String getTmplTypeInfoPg() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageNumber = request.getParameter("page");
		String pageSize = request.getParameter("rows");
		Map<String, Object> param = new HashMap<String, Object>();
		if (StringUtils.isNotEmpty(findTypeTitle)) {
			param.put("title", "%"+findTypeTitle+"%");
		}
		if (StringUtils.isNotEmpty(findTypeStId)) {
			param.put("st_id", findTypeStId);
		}
		if (StringUtils.isNotBlank(findPubFlg)) {
			param.put("publish", findPubFlg);
		}
		Page page = yjxTmplTypeService.pageTmplTypeInfo(param,
				Integer.parseInt(pageNumber),
				Integer.parseInt(pageSize));
		HashMap<String, Object> resMap = new HashMap<String, Object>();
		resMap.put("total", page.getTotalCount());
		resMap.put("rows", page.getResult());
		tmplTypeMap = resMap;
		return SUCCESS;
	}
	
	// ** 新建分类
	private YjxShowType showType;
	public void addTmplType() throws Exception {
		if (showType != null) {
			Map<String, Object> session = ActionContext.getContext().getSession();
	    	UserInfoDto user = (UserInfoDto)session.get("user");
	    	if (user != null) {
	    		showType.setUpdate_by(user.getUser_id().toString());
	    	}
	    	if (showType.getSt_id()!=null && showType.getSt_id()>0){
	    		// 修改模板组信息
	    		this.updTmplSet();
	    	} else {
	    		// 新建
	    		yjxTmplTypeService.addTmplType(showType);
	    		this.writeJSON("ok");
	    	}
		}
	}
	
	// 修改分类
	public void updTmplSet() throws Exception {
		try {
			yjxTmplTypeService.upTmplType(showType);
			this.writeJSON("ok");
		} catch (Exception e) {
			this.writeJSON("no");
		}
	}
	
	// 获取 单个分类信息
	private List<YjxShowType> tmplLis;
	public String getATmplType() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		Map<String, Object> param = new HashMap<String, Object>();
		String id = request.getParameter("stId");
		if (StringUtils.isNotEmpty(id)) {
			param.put("st_id", id);
			tmplLis = yjxTmplTypeService.searchOneTmplType(param);
		}
		return SUCCESS;
	}
	
	// 发布分类
	public void pubTmplType() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		HttpServletRequest request=ServletActionContext.getRequest();
		String ids = request.getParameter("ids");
		if(user!=null && user.getUser_id()!=null){
			if (StringUtils.isNotBlank(ids)) {
	    		List<Long> listId = StringUtils.stringToList2(ids);
	    		Map<String, Object> parm =new HashMap<String, Object>();
	    		parm.put("publish", 1);
	    		parm.put("ids", StringUtils.listToString2(listId));
	    		parm.put("update_by", user.getUser_id());
	    		yjxTmplTypeService.upTmplType(parm);
	    		
	    	}
		}
		this.writeJSON("ok");
	}
	
	// 取消发布分类
	public void cancelPubTmplType() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		HttpServletRequest request=ServletActionContext.getRequest();
		String ids = request.getParameter("ids");
		if(user!=null && user.getUser_id()!=null){
			if (StringUtils.isNotBlank(ids)) {
	    		List<Long> listId = StringUtils.stringToList2(ids);
	    		Map<String, Object> parm =new HashMap<String, Object>();
	    		parm.put("publish", 0);
	    		parm.put("ids", StringUtils.listToString2(listId));
	    		parm.put("update_by", user.getUser_id());
	    		yjxTmplTypeService.upTmplType(parm);
	    	}
		}
		this.writeJSON("ok");
	}
	
	// 修改分类顺序
	public void upTmplTyptOrder() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	if(user!=null && user.getUser_id()!=null){
    		HttpServletRequest request = ServletActionContext.getRequest();
    		String stId = request.getParameter("stId");
    		String order = request.getParameter("order");
	    	Map<String, Object> param = new HashMap<String, Object>();
			param.put("order", Integer.valueOf(order));
			param.put("st_id", Long.valueOf(stId));
			param.put("update_by", user.getUser_id());
			yjxTmplTypeService.upTmplType(param);
			this.writeJSON("ok");
    	}		
	}
	
	// ***获取一键秀分类关联列表  
	private Map<String, Object> tmplR =new HashMap<String, Object>();
	public String getTmplTypeRInfo() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageNumber = request.getParameter("page");
			String pageSize = request.getParameter("rows");
			Integer st_id = Integer.parseInt(request.getParameter("stId"));
			if(st_id != null){
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("st_id",st_id);
				Page page = yjxTmplTypeService.getTmplTypeRTmplSet(param,
							Integer.parseInt(pageNumber),
							Integer.parseInt(pageSize));
				HashMap<String, Object> resMap = new HashMap<String, Object>();
				resMap.put("total", page.getTotalCount());
				resMap.put("rows", page.getResult());
				tmplR = resMap;
			}
		}
		return SUCCESS;
	}
	
	
	
	
	// 分页添加适合的 模板组列表
	private Map<String, Object> tmplSetMap =new HashMap<String, Object>();
	private String findTSName;// 模板组标题
	private String findTSId; // 获取模板组ID
	private String findTSPub; // 模板组状态
	private String findUse;// 用途
	
	public String getTmplSetInfoPg() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageNumber = request.getParameter("page");
		String pageSize = request.getParameter("rows");
		String stId = request.getParameter("stId");
		Map<String, Object> param = new HashMap<String, Object>();
		if (StringUtils.isNotEmpty(findTSName)) {
			param.put("title", "%"+findTSName+"%");
		}
		if (StringUtils.isNotEmpty(findTSId)) {
			param.put("set_id", findTSId);
		}
		if (StringUtils.isNotBlank(findTSPub)) {
			param.put("publish", findTSPub);
		}
		if (StringUtils.isNotEmpty(findUse)){
			param.put("use",findUse);
		}
		
		HashMap<String, Object> resMap = new HashMap<String, Object>();
		if(stId != null && Integer.parseInt(stId)>0){
			param.put("st_id",stId);
			Page page = yjxTmplTypeService.pageTmplSetInfo(param,
					Integer.parseInt(pageNumber),
					Integer.parseInt(pageSize));
			resMap.put("total", page.getTotalCount());
			resMap.put("rows", page.getResult());
		}
		tmplSetMap = resMap;
		return SUCCESS;
	}
	public Map<String, Object> getTmplSetMap() {
		return tmplSetMap;
	}

	public void setTmplSetMap(Map<String, Object> tmplSetMap) {
		this.tmplSetMap = tmplSetMap;
	}

	// 给分类添加适合的 模板组
	public void addSuitTmplSet() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			String stId = request.getParameter("stId");// 分类Id
			String set_id = request.getParameter("set_id");
			if(Integer.parseInt(stId) >0){
				List<Long> list = StringUtils.stringToList2(set_id);
				boolean  b = false;//定义判断是否完成
				for (int i = 0; i < list.size(); i++) {
					Map<String, Object> param = new HashMap<String, Object>();
					param.put("st_id",stId);
					param.put("update_by",user.getUser_id());
					if (Integer.parseInt(list.get(i).toString())>0){
						param.put("set_id", list.get(i));
						b=yjxTmplTypeService.addSuitTmplSet(param);
					}
				}
				if(b){
					this.writeJSON("ok");
				}else{
					this.writeJSON("no");
				}
			}
		} else {
			this.writeJSON("no");
		}
	}
	
	// 删除分类下的模板组 关联表  ***yjx_show_type_r
	public void delTmplTypeTS() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			String rId = request.getParameter("rId");
			if(rId != null){
				Map<String, Object> param = new HashMap<String, Object>();
				List<Long> list = StringUtils.stringToList2(rId);
				param.put("ids",StringUtils.listToString2(list));
				param.put("update_by",user.getUser_id());
				boolean b=yjxTmplTypeService.delTmplTypeTS(param);
				if(b){
					this.writeJSON("ok");
				}else{
					this.writeJSON("no");
				}
			}
		}
	}
	
	// 删除一键秀 分类
	public void delTmplType() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	HttpServletRequest request = ServletActionContext.getRequest();
    	String ids = request.getParameter("ids");
    	if(user!=null && user.getUser_id()>0 && ids!=null) {
    		Map<String, Object> parm =new HashMap<String, Object>();
    		String setId = StringUtils.listToString2(StringUtils.stringToList2(ids));
    		if (Integer.parseInt(setId)>0){
    			parm.put("st_id",setId);
    			boolean b = yjxTmplTypeService.delTmplType(parm);
    			if (b){
    				this.writeJSON("ok");
    			} else{
    				this.writeJSON("no");
    			}
    		}
    	} else{
    		this.writeJSON("no");
    	}
	}

	public YjxTmplTypeService getYjxTmplTypeService() {
		return yjxTmplTypeService;
	}

	public void setYjxTmplTypeService(YjxTmplTypeService yjxTmplTypeService) {
		this.yjxTmplTypeService = yjxTmplTypeService;
	}

	public String getFindPubFlg() {
		return findPubFlg;
	}

	public void setFindPubFlg(String findPubFlg) {
		this.findPubFlg = findPubFlg;
	}

	public String getFindTypeTitle() {
		return findTypeTitle;
	}

	public void setFindTypeTitle(String findTypeTitle) {
		this.findTypeTitle = findTypeTitle;
	}

	public String getFindTypeStId() {
		return findTypeStId;
	}

	public void setFindTypeStId(String findTypeStId) {
		this.findTypeStId = findTypeStId;
	}
	public Map<String, Object> getTmplTypeMap() {
		return tmplTypeMap;
	}

	public void setTmplTypeMap(Map<String, Object> tmplTypeMap) {
		this.tmplTypeMap = tmplTypeMap;
	}

	public YjxShowType getShowType() {
		return showType;
	}

	public void setShowType(YjxShowType showType) {
		this.showType = showType;
	}

	public List<YjxShowType> getTmplLis() {
		return tmplLis;
	}

	public void setTmplLis(List<YjxShowType> tmplLis) {
		this.tmplLis = tmplLis;
	}

	public Map<String, Object> getTmplR() {
		return tmplR;
	}

	public void setTmplR(Map<String, Object> tmplR) {
		this.tmplR = tmplR;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getFindTSName() {
		return findTSName;
	}

	public void setFindTSName(String findTSName) {
		this.findTSName = findTSName;
	}

	public String getFindTSId() {
		return findTSId;
	}
	public void setFindTSId(String findTSId) {
		this.findTSId = findTSId;
	}
	public String getFindTSPub() {
		return findTSPub;
	}
	public void setFindTSPub(String findTSPub) {
		this.findTSPub = findTSPub;
	}
	public String getFindUse() {
		return findUse;
	}
	public void setFindUse(String findUse) {
		this.findUse = findUse;
	}
	
}
